package com.hand.demo.api.controller.v1;

import io.choerodon.core.domain.Page;
import io.choerodon.core.iam.ResourceLevel;
import io.choerodon.mybatis.pagehelper.annotation.SortDefault;
import io.choerodon.mybatis.pagehelper.domain.PageRequest;
import io.choerodon.mybatis.pagehelper.domain.Sort;
import io.choerodon.swagger.annotation.Permission;
import io.swagger.annotations.ApiOperation;
import org.hzero.core.base.BaseController;
import org.hzero.core.util.Results;
import org.hzero.mybatis.helper.SecurityTokenHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import com.hand.demo.app.service.HhrTaTrainingService;
import com.hand.demo.domain.entity.HhrTaTraining;
import com.hand.demo.domain.repository.HhrTaTrainingRepository;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

/**
 * 培训经历表(HhrTaTraining)表控制层
 *
 * @author
 * @since 2025-06-16 12:16:51
 */

@RestController("hhrTaTrainingController.v1")
@RequestMapping("/v1/{organizationId}/hhr-ta-trainings")
public class HhrTaTrainingController extends BaseController {

    @Autowired
    private HhrTaTrainingRepository hhrTaTrainingRepository;

    @Autowired
    private HhrTaTrainingService hhrTaTrainingService;

    @ApiOperation(value = "培训经历表列表")
    @Permission(level = ResourceLevel.ORGANIZATION)
    @GetMapping
    public ResponseEntity<Page<HhrTaTraining>> list(HhrTaTraining hhrTaTraining, @PathVariable Long organizationId,
                                                    @ApiIgnore @SortDefault(value = HhrTaTraining.FIELD_ID,
                                                            direction = Sort.Direction.DESC) PageRequest pageRequest) {
        Page<HhrTaTraining> list = hhrTaTrainingService.selectList(pageRequest, hhrTaTraining);
        return Results.success(list);
    }

    @ApiOperation(value = "培训经历表明细")
    @Permission(level = ResourceLevel.ORGANIZATION)
    @GetMapping("/{id}/detail")
    public ResponseEntity<HhrTaTraining> detail(@PathVariable Long id) {
        HhrTaTraining hhrTaTraining = hhrTaTrainingRepository.selectByPrimary(id);
        return Results.success(hhrTaTraining);
    }

    @ApiOperation(value = "创建或更新培训经历表")
    @Permission(level = ResourceLevel.ORGANIZATION)
    @PostMapping
    public ResponseEntity<List<HhrTaTraining>> save(@PathVariable Long organizationId, @RequestBody List<HhrTaTraining> hhrTaTrainings) {
        validObject(hhrTaTrainings);
        SecurityTokenHelper.validTokenIgnoreInsert(hhrTaTrainings);
        hhrTaTrainings.forEach(item -> item.setTenantId(organizationId));
        hhrTaTrainingService.saveData(hhrTaTrainings);
        return Results.success(hhrTaTrainings);
    }

    @ApiOperation(value = "删除培训经历表")
    @Permission(level = ResourceLevel.ORGANIZATION)
    @DeleteMapping
    public ResponseEntity<?> remove(@RequestBody List<HhrTaTraining> hhrTaTrainings) {
        SecurityTokenHelper.validToken(hhrTaTrainings);
        hhrTaTrainingRepository.batchDeleteByPrimaryKey(hhrTaTrainings);
        return Results.success();
    }

}

